#ifndef _GLOBAL_THREAD_DATA_H
#define	_GLOBAL_THREAD_DATA_H

#include <pthread.h>
#include <string>
using std::string;
static pthread_key_t proccessId_key;
static pthread_key_t parentProccessId_key;
static pthread_key_t descriptorTable_key;
static pthread_key_t pwd_key;

int getProccessID(){
	return *((int*) pthread_getspecific(proccessId_key));
}

int getParentProccessID(){
	int res(0);
	int* tmp_res=(int*) pthread_getspecific(parentProccessId_key);
	if(tmp_res!=NULL){
		res=*tmp_res;
	}
	return res;
}

int* getDescriptorTable(){
	return (int*) pthread_getspecific(descriptorTable_key);
}

string* getPWD(){
	return (string*) pthread_getspecific(pwd_key);
}

void destroyProccessID(void *value) {
	if (value != NULL){
		free(value);
	}
	pthread_setspecific(proccessId_key, NULL);
}

void destroyParentProccessID(void *value) {
	if (value != NULL){
		free(value);
	}
	pthread_setspecific(parentProccessId_key, NULL);
}

void destroyDescriptorTable(void *value) {
	if (value != NULL){
		free(value);
	}
	pthread_setspecific(descriptorTable_key, NULL);
}

void destroyPWD(void *value) {
	if (value != NULL){
		free(value);
	}
	pthread_setspecific(pwd_key, NULL);
}

void initThreadKeys(){
	pthread_key_create(&proccessId_key, destroyProccessID);
	pthread_key_create(&parentProccessId_key, destroyParentProccessID);
	pthread_key_create(&descriptorTable_key, destroyDescriptorTable);
	pthread_key_create(&pwd_key, destroyPWD);
}

void destroyThreadKeys(){
	pthread_key_delete(proccessId_key);
	pthread_key_delete(parentProccessId_key);
	pthread_key_delete(descriptorTable_key);
	pthread_key_delete(pwd_key);
}



#endif
